﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HP.Cachalote.WPF.Model;
using HP.Cachalote.WPF.ViewModel;
using HP.Cachalote.DataModels;
using HP.Cachalote.Services.Shared;
using HP.Cachalote.Services.Interfaces;

namespace HP.Cachalote.WPF.Shared
{
    public static class ViewModelHelper
    {
        /// <summary>
        /// Create a new instance of UnfinishedTodoItemTreeViewItemViewModel based on provided the instance of TodoItem.
        /// </summary>
        /// <param name="todoItem">The provided the instance of TodoItem, it MUST contains the data of its Label.</param>
        /// <param name="dataService">An instance of IDataService that used to get ServiceFactory.</param>
        /// <returns>A new instance of UnfinishedTodoItemTreeViewItemViewModel</returns>
        public static TodoItemTreeViewItemViewModel CreateNewUnfinishedTodoItemTreeViewItemViewModel(IDataService dataService, TodoItem todoItem)
        {
            var newItem = new TodoItemTreeViewItemViewModel(dataService)
            {
                TodoItemID = todoItem.ID,
                Title = todoItem.Title,
                Note = todoItem.Note,
                Priority = todoItem.Priority,
                DueDate = todoItem.DueDate,
                StartTime = todoItem.StartTime,
                EndTime = todoItem.EndTime,
                TodoLabelName = todoItem.Label.Name,
                IsMatched = true,
                DueTimeDisplayViewModel = new DueTimeDisplayViewModel(dataService)
                {
                    TodoItemID = todoItem.ID,
                    DueDate = todoItem.DueDate,
                    StartTime = todoItem.StartTime,
                    EndTime = todoItem.EndTime,
                    DueDateDisplay = InformationFormatter.FormatDueDateDisplay(todoItem.DueDate),
                    DueTimeDisplay = InformationFormatter.FormatDueTimeDisplay(todoItem.DueDate, todoItem.StartTime, todoItem.EndTime),
                    DueDateStatus = InformationFormatter.GetDueDateStatus(todoItem.DueDate, todoItem.StartTime, todoItem.EndTime)
                },
                DueTimePickerViewModel = new DueTimePickerViewModel(dataService)
                {
                    TodoItemID = todoItem.ID,
                    TodoItemTitle = todoItem.Title,
                    DueDate = todoItem.DueDate.HasValue ? todoItem.DueDate : GeneralUtilities.GetCurrentDateTime(),
                    StartTime = todoItem.StartTime,
                    EndTime = todoItem.EndTime,
                    IsStartTimeSpecified = todoItem.StartTime.HasValue, 
                    IsEndTimeSpecified = todoItem.EndTime.HasValue, 
                    IsAllDayEvent = !todoItem.StartTime.HasValue && !todoItem.EndTime.HasValue ? true : false
                },
                EditNoteViewModel = new EditNoteViewModel(dataService)
                {
                    TodoItemID = todoItem.ID,
                    TodoItemTitle = todoItem.Title,
                    InitializedNote = todoItem.Note,
                    Note = todoItem.Note
                }
            };

            return newItem;
        }
    }
}
